package com.syni.mdd.yhd.common.all.controller.api.business;

import com.alibaba.fastjson.JSON;
import com.syni.mdd.yhd.common.all.constants.Constants;
import com.syni.mdd.yhd.common.all.constants.ResultCode;
import com.syni.mdd.yhd.common.all.constants.ResultMsg;
import com.syni.mdd.yhd.common.all.entity.BmsBusiness;
import com.syni.mdd.yhd.common.all.entity.BmsUserBusinessAuth;
import com.syni.mdd.yhd.common.all.entity.user.BmsUser;
import com.syni.mdd.yhd.common.all.service.*;
import com.syni.mdd.yhd.common.all.utils.SpecificationUtils;
import com.syni.mdd.sjb.common.component.utils.common.utils.BeanUtils;
import com.syni.mdd.sjb.common.component.utils.common.utils.RpcResponseVo;
import com.syni.mdd.sjb.common.component.utils.common.utils.StringUtils;
import com.syni.mdd.sjb.common.component.utils.feign.dto.mddAll.apiBusiness.GetUserClaimBusinessRpDto;
import com.syni.mdd.sjb.common.component.utils.feign.dto.mddAll.apiBusinessAuth.GetBusinessAuthByLicenseNameDto;
import com.syni.mdd.sjb.common.component.utils.feign.service.mddAll.BusinessAuthRestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/apiBusinessAuth")
public class BusinessAuthRestApi implements BusinessAuthRestService {

    @Autowired
    BmsUserBusinessAuthService bmsUserBusinessAuthService;

    @Autowired
    BmsUserService bmsUserService;

    @Autowired
    BmsBusinessLabelService bmsBusinessLabelService;

    @Autowired
    BmsBusinessTypeService bmsBusinesseTypeService;

    @Autowired
    BmsBusinessTypeRelationService bmsBusinessTypeRelationService;

    @Override
    @PostMapping("/getBusinessAuth")
    public RpcResponseVo getBusinessAuth(Integer businessId) {
        BmsUserBusinessAuth bmsUserBusinessAuth = bmsUserBusinessAuthService.findByBusinessIdNotReject(businessId);
        if(bmsUserBusinessAuth.getBmsBusiness() != null){
            bmsBusinessTypeRelationService.postBmsBusinessTypeId(bmsUserBusinessAuth.getBmsBusiness());
            bmsBusinesseTypeService.postHandleBusinessType(bmsUserBusinessAuth.getBmsBusiness());
        }
        if(bmsUserBusinessAuth == null){
            return RpcResponseVo.result(ResultCode.CODE_ERROR_05, ResultMsg.CODE_ERROR_05);
        }
        return RpcResponseVo.success(bmsUserBusinessAuth);
    }

    @Override
    @GetMapping("/getBusinessAuthByLicenseName")
    public RpcResponseVo getBusinessAuthByLicenseName(@RequestParam("licenseName") String licenseName) {
        List<BmsUserBusinessAuth> bmsUserBusinessAuths = bmsUserBusinessAuthService.getBaseDao().findByLicenseName(licenseName);
        List<GetBusinessAuthByLicenseNameDto> getBusinessAuthByLicenseNameDtos = BeanUtils.copyList(bmsUserBusinessAuths,GetBusinessAuthByLicenseNameDto.class);
        return RpcResponseVo.success(getBusinessAuthByLicenseNameDtos,ResultMsg.MSG_GET_SUCCESS);
    }

    @Override
    @GetMapping("/getBusinessAuthByLicenseNameAndPhone")
    public RpcResponseVo getBusinessAuthByLicenseNameAndPhone(@RequestParam("licenseName") String licenseName,@RequestParam("phone") String phone) {
        BmsUser bmsUser = bmsUserService.findByPhone(phone);
        if(bmsUser == null){
            return RpcResponseVo.result(ResultCode.CODE_ERROR_05,ResultMsg.CODE_ERROR_05);
        }
        List<BmsUserBusinessAuth> bmsUserBusinessAuths = bmsUserBusinessAuthService.getBaseDao().findByLicenseNameAndBmsUserId(licenseName,bmsUser.getId());
        List<GetBusinessAuthByLicenseNameDto> getBusinessAuthByLicenseNameDtos = BeanUtils.copyList(bmsUserBusinessAuths,GetBusinessAuthByLicenseNameDto.class);
        return RpcResponseVo.success(getBusinessAuthByLicenseNameDtos,ResultMsg.MSG_GET_SUCCESS);
    }

    @Override
    @GetMapping("/getUserClaimBusinessByUserIdPage")
    public RpcResponseVo getUserClaimBusinessByUserIdPage(@RequestParam("userId") Integer userId,@RequestParam("pageNum") Integer pageNum,
                                                          @RequestParam("pageSize") Integer pageSize) {
        Map<String,Object> conditionMap = new HashMap<>();
        conditionMap.put("bmsUserId",userId);
        Pageable pageable = BmsCommonService.getPageable(pageNum,pageSize);
        Page<BmsUserBusinessAuth> bmsUserBusinessAuthPage = bmsUserBusinessAuthService.findUserBusinessByPageable(userId,pageable);
        List<BmsUserBusinessAuth> bmsUserBusinessAuths = bmsUserBusinessAuthPage.getContent();
        List<BmsBusiness> bmsBusinesses = bmsUserBusinessAuths.stream().map(BmsUserBusinessAuth::getBmsBusiness).collect(Collectors.toList());
        bmsBusinessTypeRelationService.postBmsBusinessTypeId(bmsBusinesses);
        bmsBusinesseTypeService.postHandleBusinessType(bmsBusinesses);
        //组装数据
        List<GetUserClaimBusinessRpDto> getUserClaimBusinessRpDtos = new ArrayList<>();
        bmsUserBusinessAuths.forEach(bu -> {
            GetUserClaimBusinessRpDto getUserClaimBusinessRpDto = BeanUtils.copyProperties(bu,GetUserClaimBusinessRpDto.class);
            BeanUtils.copyProperties(bu.getBmsBusiness(),getUserClaimBusinessRpDto);
            getUserClaimBusinessRpDto.setBusinessId(bu.getBmsBusinessId());
            getUserClaimBusinessRpDto.setTypeId(bu.getBmsBusiness().getTagsId());
            getUserClaimBusinessRpDto.setTypeName(bu.getBmsBusiness().getTags());
            getUserClaimBusinessRpDto.setLabelName(bu.getBmsBusiness().getLabel());
            getUserClaimBusinessRpDto.setBackColor(bu.getBmsBusiness().getBmsBusinessType().getBackColor());
            getUserClaimBusinessRpDto.setFontColor(bu.getBmsBusiness().getBmsBusinessType().getFontColor());
            getUserClaimBusinessRpDtos.add(getUserClaimBusinessRpDto);
        });
        Map<String,Object> userData = new HashMap<>();
        userData.put("pageNum",pageNum);
        userData.put("pageSize",pageSize);
        userData.put("count",bmsUserBusinessAuthPage.getTotalElements());
        return RpcResponseVo.success(getUserClaimBusinessRpDtos,userData,ResultMsg.MSG_GET_SUCCESS);
    }

    @Override
    @GetMapping("/getUserClaimBusinessByBusinessAuthIds")
    public RpcResponseVo getUserClaimBusinessByBusinessAuthIds(@RequestParam("businessAuthIds") String businessAuthIds) {
        //组装数据
        List<GetUserClaimBusinessRpDto> getUserClaimBusinessRpDtos = new ArrayList<>();
        if(StringUtils.isBlank(businessAuthIds)){
            return RpcResponseVo.success(getUserClaimBusinessRpDtos,ResultMsg.MSG_GET_SUCCESS);
        }
        Set<Integer> ids = new HashSet<>(JSON.parseArray(businessAuthIds, Integer.class));
        Map<String,Object> conditionMap = new HashMap<>();
        conditionMap.put("id_IN",ids);
        List<BmsUserBusinessAuth> bmsUserBusinessAuths = bmsUserBusinessAuthService.getBaseDao().findAll(new SpecificationUtils(conditionMap));
        List<BmsBusiness> bmsBusinesses = bmsUserBusinessAuths.stream().map(BmsUserBusinessAuth::getBmsBusiness).collect(Collectors.toList());
        bmsBusinessTypeRelationService.postBmsBusinessTypeId(bmsBusinesses);
        bmsBusinesseTypeService.postHandleBusinessType(bmsBusinesses);
        bmsUserBusinessAuths.forEach(bu -> {
            GetUserClaimBusinessRpDto getUserClaimBusinessRpDto = BeanUtils.copyProperties(bu,GetUserClaimBusinessRpDto.class);
            BeanUtils.copyProperties(bu.getBmsBusiness(),getUserClaimBusinessRpDto);
            getUserClaimBusinessRpDto.setBusinessId(bu.getBmsBusinessId());
            getUserClaimBusinessRpDto.setTypeId(bu.getBmsBusiness().getTagsId());
            getUserClaimBusinessRpDto.setTypeName(bu.getBmsBusiness().getTags());
            getUserClaimBusinessRpDto.setLabelName(bu.getBmsBusiness().getLabel());
            getUserClaimBusinessRpDto.setBackColor(bu.getBmsBusiness().getBmsBusinessType().getBackColor());
            getUserClaimBusinessRpDto.setFontColor(bu.getBmsBusiness().getBmsBusinessType().getFontColor());
            getUserClaimBusinessRpDtos.add(getUserClaimBusinessRpDto);
        });
        return RpcResponseVo.success(getUserClaimBusinessRpDtos,ResultMsg.MSG_GET_SUCCESS);
    }

    @GetMapping("/getUserClaimBusinessByBusinessAuthId")
    public RpcResponseVo getUserClaimBusinessByBusinessAuthId(@RequestParam("businessAuthId") Integer businessAuthId) {
        //组装数据
        List<GetUserClaimBusinessRpDto> getUserClaimBusinessRpDtos = new ArrayList<>();
        Map<String,Object> conditionMap = new HashMap<>();
        conditionMap.put("id",businessAuthId);
        List<BmsUserBusinessAuth> bmsUserBusinessAuths = bmsUserBusinessAuthService.getBaseDao().findAll(new SpecificationUtils(conditionMap));
        List<BmsBusiness> bmsBusinesses = bmsUserBusinessAuths.stream().map(BmsUserBusinessAuth::getBmsBusiness).collect(Collectors.toList());
        bmsBusinessTypeRelationService.postBmsBusinessTypeId(bmsBusinesses);
        bmsBusinesseTypeService.postHandleBusinessType(bmsBusinesses);
        bmsUserBusinessAuths.forEach(bu -> {
            GetUserClaimBusinessRpDto getUserClaimBusinessRpDto = BeanUtils.copyProperties(bu,GetUserClaimBusinessRpDto.class);
            BeanUtils.copyProperties(bu.getBmsBusiness(),getUserClaimBusinessRpDto);
            getUserClaimBusinessRpDto.setBusinessId(bu.getBmsBusinessId());
            getUserClaimBusinessRpDto.setTypeId(bu.getBmsBusiness().getTagsId());
            getUserClaimBusinessRpDto.setTypeName(bu.getBmsBusiness().getTags());
            getUserClaimBusinessRpDto.setLabelName(bu.getBmsBusiness().getLabel());
            getUserClaimBusinessRpDto.setBackColor(bu.getBmsBusiness().getBmsBusinessType().getBackColor());
            getUserClaimBusinessRpDto.setFontColor(bu.getBmsBusiness().getBmsBusinessType().getFontColor());
            getUserClaimBusinessRpDtos.add(getUserClaimBusinessRpDto);
        });
        return RpcResponseVo.success(getUserClaimBusinessRpDtos,ResultMsg.MSG_GET_SUCCESS);
    }

    @Override
    @GetMapping("/getUserClaimBusinessByBusinessId")
    public RpcResponseVo getUserClaimBusinessByBusinessId(@RequestParam("businessId") Integer businessId) {
        Map<String,Object> conditionMap = new HashMap<>();
        conditionMap.put("bmsBusinessId",businessId);
        BmsUserBusinessAuth bmsUserBusinessAuth = bmsUserBusinessAuthService.findByBusinessIdNotReject(businessId);
        BmsBusiness bmsBusiness = bmsUserBusinessAuth.getBmsBusiness();
        bmsBusinessTypeRelationService.postBmsBusinessTypeId(bmsBusiness);
        bmsBusinesseTypeService.postHandleBusinessType(bmsBusiness);
        //组装数据
        GetUserClaimBusinessRpDto getUserClaimBusinessRpDto = BeanUtils.copyProperties(bmsUserBusinessAuth,GetUserClaimBusinessRpDto.class);
        BeanUtils.copyProperties(bmsBusiness,getUserClaimBusinessRpDto);
        getUserClaimBusinessRpDto.setBusinessId(bmsBusiness.getId());
        getUserClaimBusinessRpDto.setTypeId(bmsBusiness.getTagsId());
        getUserClaimBusinessRpDto.setTypeName(bmsBusiness.getTags());
        getUserClaimBusinessRpDto.setLabelName(bmsBusiness.getLabel());
        getUserClaimBusinessRpDto.setBackColor(bmsBusiness.getBmsBusinessType().getBackColor());
        getUserClaimBusinessRpDto.setFontColor(bmsBusiness.getBmsBusinessType().getFontColor());
        Map<String,Object> userData = new HashMap<>();
        userData.put("count",1);
        return RpcResponseVo.success(getUserClaimBusinessRpDto,userData,ResultMsg.MSG_GET_SUCCESS);
    }

    @Override
    @GetMapping("/getCertifiedStores")
    public RpcResponseVo getCertifiedStores() {
        Map<String,Object> conditionMap = new HashMap<>();
        conditionMap.put("bmsBusinessId_NN", Constants.COMMON_FLAG_TRUE_INT);
        List<BmsUserBusinessAuth> bmsUserBusinessAuth = bmsUserBusinessAuthService.getBaseDao().findAll(new SpecificationUtils(conditionMap));
        return RpcResponseVo.success(bmsUserBusinessAuth,ResultMsg.MSG_GET_SUCCESS);
    }




//    @Override
//    @GetMapping("/getUserClaimBusinessByBusinessAuthId")
//    public RpcResponseVo getUserClaimBusinessByBusinessAuthId(@RequestParam("businessAuthId") Integer businessAuthId) {
//        //组装数据
//        List<GetUserClaimBusinessRpDto> getUserClaimBusinessRpDtos = new ArrayList<>();
//        Map<String,Object> conditionMap = new HashMap<>();
//        conditionMap.put("id",businessAuthId);
//        List<BmsUserBusinessAuth> bmsUserBusinessAuths = bmsUserBusinessAuthService.getBaseDao().findAll(new SpecificationUtils(conditionMap));
//        List<BmsBusiness> bmsBusinesses = bmsUserBusinessAuths.stream().map(BmsUserBusinessAuth::getBmsBusiness).collect(Collectors.toList());
//        bmsBusinessTypeRelationService.postBmsBusinessTypeId(bmsBusinesses);
//        bmsBusinesseTypeService.postHandleBusinessType(bmsBusinesses);
//        bmsUserBusinessAuths.forEach(bu -> {
//            GetUserClaimBusinessRpDto getUserClaimBusinessRpDto = BeanUtils.copyProperties(bu,GetUserClaimBusinessRpDto.class);
//            BeanUtils.copyProperties(bu.getBmsBusiness(),getUserClaimBusinessRpDto);
//            getUserClaimBusinessRpDto.setBusinessId(bu.getBmsBusinessId());
//            getUserClaimBusinessRpDto.setTypeId(bu.getBmsBusiness().getTagsId());
//            getUserClaimBusinessRpDto.setTypeName(bu.getBmsBusiness().getTags());
//            getUserClaimBusinessRpDto.setLabelName(bu.getBmsBusiness().getLabel());
//            getUserClaimBusinessRpDto.setBackColor(bu.getBmsBusiness().getBmsBusinessType().getBackColor());
//            getUserClaimBusinessRpDto.setFontColor(bu.getBmsBusiness().getBmsBusinessType().getFontColor());
//            getUserClaimBusinessRpDtos.add(getUserClaimBusinessRpDto);
//        });
//        return RpcResponseVo.success(getUserClaimBusinessRpDtos,ResultMsg.MSG_GET_SUCCESS);
//    }
}
